﻿using System;
using System.Text;
using System.Runtime.Serialization.Json;
using System.IO;

namespace Alex.Library.Web.Ajax
{
    /// <summary>
    /// Json序列化工具类
    /// </summary>
    class JsonSerializeHelper : SerializeHelper
    {
        public override bool IncludeNull { get; set; }
        
        /// <summary>
        /// 将对象序列化为JSON字符串
        /// </summary>
        /// <typeparam name="T">对象类型</typeparam>
        /// <param name="obj">对象</param>
        /// <param name="encoding">字符编码</param>
        /// <returns></returns>
        public override string Serialize<T>(T obj, Encoding encoding)
        {
            try
            {
                DataContractJsonSerializer json = new DataContractJsonSerializer(typeof(T));
                using (MemoryStream ms = new MemoryStream())
                {
                    json.WriteObject(ms, obj);

                    string result = encoding.GetString(ms.ToArray());

                    return result;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        /// <summary>
        /// 将对象序列化为JSON字符串
        /// </summary>        
        /// <param name="obj">对象</param>
        /// <param name="encoding">字符编码</param>
        /// <returns></returns>
        public override string Serialize(object obj, Encoding encoding)
        {
            try
            {
                DataContractJsonSerializer json = new DataContractJsonSerializer(obj.GetType());                
                using (MemoryStream ms = new MemoryStream())
                {
                    json.WriteObject(ms, obj);

                    string result = encoding.GetString(ms.ToArray());

                    return result;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        /// <summary>
        /// 将JSON字符串中反序列化为对象
        /// </summary>
        /// <typeparam name="T">对象类型</typeparam>
        /// <param name="serializeString">JSON字符串</param>
        /// <param name="encoding">字符编码</param>
        /// <returns></returns>
        public override T Deserialize<T>(string serializeString, Encoding encoding)
        {
            try
            {
                T obj = System.Activator.CreateInstance<T>();
                byte[] buffer = encoding.GetBytes(serializeString);
                using (MemoryStream ms = new MemoryStream(buffer))
                {
                    DataContractJsonSerializer json = new DataContractJsonSerializer(typeof(T));
                    return (T)json.ReadObject(ms);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        /// <summary>
        /// 将JSON字符串中反序列化为对象
        /// </summary>
        /// <param name="serializeString">JSON字符串</param>
        /// <param name="type">反序列化对象的类型</param>
        /// <param name="encoding">字符编码</param>
        /// <returns></returns>
        public override object Deserialize(string serializeString, Type type, Encoding encoding)
        {
            try
            {
                object obj = System.Activator.CreateInstance(type);
                byte[] buffer = encoding.GetBytes(serializeString);
                using (MemoryStream ms = new MemoryStream(buffer))
                {
                    DataContractJsonSerializer json = new DataContractJsonSerializer(type);
                    return json.ReadObject(ms);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
    }
}
